package com.glr.company.service;

import com.glr.company.dao.CompanyRepository;
import com.glr.company.entity.CompanyEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 公司模块业务层
 *
 * @author GLoRy
 * @date 2021/6/1 10:17
 */
@Service
public class CompanyService {

    @Autowired
    private CompanyRepository companyRepository;

    /**
     * 查询所有的企业
     *
     * @return 所有企业集合
     */
    public List<CompanyEntity> findAll() {
        return companyRepository.findAll();
    }

    /**
     * 查询所有的企业并分页
     *
     * @param page     当前页
     * @param pageSize 每页条数
     * @return 所有企业分页信息
     */
    public Page<CompanyEntity> findByPage(int page, int pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        return companyRepository.findAll(pageRequest);
    }

    /**
     * 条件查询
     *
     * @param conditionMap 条件Map
     * @return 条件查询结果
     */
    public List<CompanyEntity> findByCondition(Map<?, ?> conditionMap) {
        Specification<CompanyEntity> specification = conditionSpecification(conditionMap);
        return companyRepository.findAll(specification);
    }

    /**
     * 带分页的条件查询
     *
     * @param conditionMap 条件Map
     * @param page         当前页
     * @param pageSize     每页条数
     * @return 分页条件查询结果
     */
    public Page<CompanyEntity> findByCondition(Map<?, ?> conditionMap, int page, int pageSize) {
        Specification<CompanyEntity> specification = conditionSpecification(conditionMap);
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        return companyRepository.findAll(specification, pageRequest);
    }

    /**
     * 动态规格查询构建
     *
     * @param conditionMap 条件Map
     * @return 标准生成器
     */
    private Specification<CompanyEntity> conditionSpecification(Map<?, ?> conditionMap) {
        return (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            String shortName = "shortName";
            String fullName = "fullName";
            String remark = "remark";
            if (conditionMap.get(shortName) != null && !"".equals(conditionMap.get(shortName))) {
                predicateList.add(criteriaBuilder.like(root.get("shortName").as(String.class), "%" + conditionMap.get("shortName") + "%"));
            }
            if (conditionMap.get(fullName) != null && !"".equals(conditionMap.get(fullName))) {
                predicateList.add(criteriaBuilder.like(root.get("fullName").as(String.class), "%" + conditionMap.get("fullName") + "%"));
            }
            if (conditionMap.get(remark) != null && !"".equals(conditionMap.get(remark))) {
                predicateList.add(criteriaBuilder.like(root.get("remark").as(String.class), "%" + conditionMap.get("remark") + "%"));
            }
            return criteriaBuilder.and(predicateList.toArray(new Predicate[0]));
        };
    }

    /**
     * 企业的增加
     *
     * @param company 公司
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(CompanyEntity company) {
        companyRepository.save(company);
    }

    /**
     * 企业的修改
     *
     * @param company 公司
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CompanyEntity company) {
        companyRepository.save(company);
    }

    /**
     * 企业的启动
     *
     * @param companyId 公司id
     */
    @Transactional(rollbackFor = Exception.class)
    public void enable(Integer companyId) {
        companyRepository.enable(companyId);
    }

    /**
     * 企业的禁用
     *
     * @param companyId 公司id
     */
    @Transactional(rollbackFor = Exception.class)
    public void disable(Integer companyId) {
        companyRepository.disable(companyId);
    }

    /**
     * 企业的删除
     *
     * @param id 公司id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Integer id) {
        companyRepository.deleteById(id);
    }
}
